package erwiki.ui.app.handlers;

import java.util.Collections;
import java.util.List;

import org.apache.log4j.Logger;
import org.eclipse.e4.core.di.annotations.Execute;
import org.eclipse.e4.ui.model.application.MApplication;
import org.eclipse.e4.ui.model.application.ui.advanced.MPerspective;
import org.eclipse.e4.ui.model.application.ui.menu.MMenu;
import org.eclipse.e4.ui.workbench.modeling.EModelService;
import org.eclipse.e4.ui.workbench.modeling.EPartService;

import jakarta.inject.Inject;

public class SwitchToPerspective {

	private static final Logger log = Logger.getLogger(SwitchToPerspective.class);

	protected static String ID_PERSPECTIVE_ADMIN = "erwiki.ui.app.admin.perspective.admin";

	@Inject
	EModelService modelService;
	@Inject
	MApplication application;

	@Execute
	// public void execute(MApplication app, EPartService partService, EModelService
	// modelService) {
	public void switchPersepctive(MPerspective activePerspective, MApplication app, EPartService partService,
			EModelService modelService) {
		log.debug("start action SwitchToPerspective");
		MPerspective element = (MPerspective) modelService.find(ID_PERSPECTIVE_ADMIN, app);
		// now switch perspective
		// partService.switchPerspective(element);
		

		List<MPerspective> perspectives = modelService.findElements(app, null, MPerspective.class, null);
		// Assume you have only two perspectives and you always
		// switch between them
		for (MPerspective perspective : perspectives) {
			if (!perspective.equals(activePerspective)) {
				partService.switchPerspective(perspective);

				boolean state;
				if (perspective.getElementId().equals(ID_PERSPECTIVE_ADMIN)) {
					state = true;
				} else {
					state = false;
				}

				MMenu menu1 = modelService.findElements(application, //
						"erwiki.ui.app.admin.menu.customization", //
						MMenu.class, Collections.<String>emptyList(), EModelService.IN_MAIN_MENU).get(0);

				menu1.setVisible(state);
				menu1.setToBeRendered(state);
//        		menu2.setVisible(beView);
//        		menu2.setToBeRendered(beView);
//        		menu3.setVisible(beView);
//        		menu3.setToBeRendered(beView);

			}
		}
	}

}
